package com.hm.service.request.test;

import com.alibaba.fastjson2.JSON;
import com.hm.service.entity.ParamsBean;
import com.hm.service.http.HttpUtils;
import com.hm.service.utils.EncryptionUtils;
import com.hm.service.utils.JsonUtils;
import okhttp3.*;

import java.io.IOException;


/**
 * 获取session接口
 */
public class Request02 {

    private static final String url = "http://172.26.2.95:8081/rick/main/000_000_002";
    //秘钥
    private static final String secretKey = "kokL8G4RmNfV69cdcJ9dfnrlFGsLy6mH";

    public static void main(String[] args) {
        //请求参数
        ParamsBean paramsBean = new ParamsBean();
        paramsBean.taskId = "100001";
        paramsBean.action = "1";
        paramsBean.cronId = "1";
        paramsBean.token = "KWElQXFbK9htGSBmoEg8shWyTrYIxG4F+gZL2dSojcOPUOb9Pa5Xuo0dRjeHXnVQ";
        String reqParams = JSON.toJSONString(paramsBean);
        //构建请求体
        RequestBody body = RequestBody.create(EncryptionUtils.encrypt(secretKey, reqParams), MediaType.get("application/json; charset=UTF-8"));
        Request request = new Request.Builder()//
                .addHeader("sessionId", "QoqXPxZmkpofQy81u4Le3BFhB+/F1sEZHPtBixL70O1pqiVuZG7zlIvyUEAE3i505itmwSzoXW8WhD3VEuoHz6UkgtQbFPLhnoS7Gc+qvVw=").url(url)//请求连接
                .post(body)//请求体
                .build();//构建
        //同步请求
        OkHttpClient okHttpClient = HttpUtils.getOkHttpClient();
        try (Response response = okHttpClient.newCall(request).execute()) {
            ResponseBody resBody = response.body();
            int code = response.code();
            if (code == 200 && resBody != null) {
                String jsonString = resBody.string();
                boolean json = JsonUtils.isJson(jsonString);
                System.out.println("ResponseBody-->:" + json);
                System.out.println(json ? jsonString : EncryptionUtils.decrypt(secretKey, jsonString));
            } else {
                System.out.println("ResponseBody为空:");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
